package com.mmall.controller.backstage;

import com.mmall.common.IConst;
import com.mmall.common.ServerResponse;
import com.mmall.pojo.User;
import com.mmall.service.IUserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

/**
 * 用户管理员控制层
 * Created by qezhhnjy on 2017/9/25-22:12.
 */
@Controller
@RequestMapping("/admin/user/")
public class UserAdminController {

    @Resource
    private IUserService iUserService;

    /**
     * 用户管理员登录
     *
     * @param username 用户名
     * @param password 用户密码
     * @param session  当前会话
     * @return 响应对象
     */
    @RequestMapping(value = "login.do", method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<User> login(String username, String password, HttpSession session) {
        ServerResponse<User> response = iUserService.login(username, password);
        if (response.isSuccess()) {
            User user = response.getData();
            if (user.getRole() == IConst.Role.ADMIN) {
                session.setAttribute(IConst.CURRENT_USER, user);
                return response;
            } else return ServerResponse.createByErrorMessage("你不是管理员,无法登录!");
        }
        return response;
    }
}
